package com.TCP_UDP网络编程.homeWork;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UDP01_server {
    public static void main(String[] args) throws IOException {
        DatagramSocket socket = new DatagramSocket(9999);
        byte[] bytes = new byte[1024];
        DatagramPacket packet = new DatagramPacket(bytes, bytes.length);
        socket.receive(packet);

        int length = packet.getLength();
        byte[] data = packet.getData();
        String s = new String(data, 0, length);
        System.out.println(s);

        if(s.equals("四大名著")){
            bytes = "四大名著是  红楼梦 西游记 三国演义 水浒传".getBytes();
        }else {
            bytes = "你bb啥呢".getBytes();
        }
        packet = new DatagramPacket(bytes,bytes.length, InetAddress.getLocalHost(),9998);
        socket.send(packet);
        socket.close();
    }
}
